package com.canbot.u05.activity.shoppingGuide.merchandiseShoppingGuide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.MerchandiseShoppingGuideWelcome;
import com.canbot.u05.network.SalesApi;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.MerchandiseWelcomeDialog;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 设置类型选择界面
 * 1.欢迎信息
 * 2.商品信息
 */
public class SelectSettingTypeActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private static final String TAG = "SelectSettingTypeActivity";

        private Button btnBack;

        private TextView tvSettingWelcomeVoice, tvSettingMerchandiseInfo;

        private MerchandiseWelcomeDialog dialog;

        private Dialog dialogLoading;

        private List<MerchandiseShoppingGuideWelcome> mList;

        private MerchandiseShoppingGuideWelcome shoppingGuideWelcome;

        private String text;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_select_setting_type);

                boolean isFirst = getIntent().getBooleanExtra("isFirst", false);
//        if (isFirst){
                boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.SHOPPING_GUIDE_DIR + "merchandise/szdr_spdg_u05_001.wav");
                if (!isSuccess) {
                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                }
//        }
                setViews();
                //获取欢迎设置
                initWelcomeInfo();

        }

        private void setViews() {
                btnBack = (Button) findViewById(R.id.shopping_guide_btn_delete);
                tvSettingWelcomeVoice = (TextView) findViewById(R.id.sg_rl_welcome);
                tvSettingMerchandiseInfo = (TextView) findViewById(R.id.sg_rl_merchandise);
                tvSettingWelcomeVoice.setOnClickListener(this);
                tvSettingMerchandiseInfo.setOnClickListener(this);
//        tvSettingWelcomeVoice.setSelected(true);

        }

        private void initWelcomeInfo() {
                mList = new ArrayList<>();
                //获取数据
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://" + IpMsgConst.SOCKET_HOST2 + ":7755")
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(setTimeOut())
                        .build();
                SalesApi salesApi = retrofit.create(SalesApi.class);
                Call<List<MerchandiseShoppingGuideWelcome>> allAction = salesApi.getMerchandiseWelcome("query");

                allAction.enqueue(new Callback<List<MerchandiseShoppingGuideWelcome>>() {
                        @Override
                        public void onResponse(Call<List<MerchandiseShoppingGuideWelcome>> call, Response<List<MerchandiseShoppingGuideWelcome>> response) {
                                Message message = Message.obtain();
                                if (response.body() != null) {
                                        Logger.e(TAG, "所有问答数据" + response.body().toString());
                                        List<MerchandiseShoppingGuideWelcome> infos = response.body();
                                        if (infos != null && infos.size() != 0) {
                                                mList.addAll(infos);
                                                shoppingGuideWelcome = mList.get(0);
                                        }
                                        //设置适配
//                    setAdapter();
                                }
                                else {
//                    handler.sendMessage(message);
                                }
//                LoadingDialogUtils.closeDialog(dialogLoading);
                        }

                        @Override
                        public void onFailure(Call<List<MerchandiseShoppingGuideWelcome>> call, Throwable t) {
//                LoadingDialogUtils.closeDialog(dialogLoading);
//                mHandler.sendEmptyMessage(NET_ERROR);
                        }
                });
        }


        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.sg_rl_welcome:
                                //欢迎语音设置
                                boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.SHOPPING_GUIDE_DIR + "merchandise/hyxxsz_spdg_u05_001.wav");
                                if (!isSuccess) {
                                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                                }
                                //弹出选择窗
                                dialog = new MerchandiseWelcomeDialog(SelectSettingTypeActivity.this, "欢迎文字", "欢迎语音");
                                dialog.setOnLeftListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                                Intent intent = new Intent(SelectSettingTypeActivity.this, WelcomActivity.class);
                                                if (shoppingGuideWelcome != null) {
                                                        Logger.e(TAG, "欢迎文字==" + shoppingGuideWelcome.getTitle());
                                                        intent.putExtra("text", shoppingGuideWelcome.getTitle());
                                                }
                                                startActivityForResult(intent, 111);
                                        }
                                });
                                dialog.setOnRightListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {

                                                dialog.dismiss();

                                                if (shoppingGuideWelcome != null ) {
                                                        Intent intent = new Intent(SelectSettingTypeActivity.this, SelectWelcomeAudioActivity.class);
                                                        Logger.e(TAG, "原有语音==" + shoppingGuideWelcome.toString());
                                                        if (!TextUtils.isEmpty(shoppingGuideWelcome.getVoice())){
                                                                intent.putExtra("edit",true);
                                                        }
                                                        intent.putExtra("welcome", JSON.toJSONString(shoppingGuideWelcome));
                                                        startActivity(intent);
                                                        finish();
                                                        overridePendingTransition(R.anim.face_zoom_enter,
                                                                R.anim.face_zoom_exit);
                                                }
                                                else {
                                                        ToastTools.newInstance(SelectSettingTypeActivity.this).showText("请先设置欢迎文字哦");
                                                }


                                        }
                                });
                                dialog.show();
                                break;
                        case R.id.sg_rl_merchandise:
                                //商品信息设置
                                boolean isSendSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.SHOPPING_GUIDE_DIR + "merchandise/spxxsz_spdg_u05_001.wav");
                                if (!isSendSuccess) {
                                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                                }
                                Intent intent = new Intent(SelectSettingTypeActivity.this, MerchandiseActivity.class);
                                startActivity(intent);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);

                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == 111 && resultCode == RESULT_OK) {
                        boolean checked = data.getBooleanExtra("isChecked", false);
                        text = data.getStringExtra("text");
                        Logger.e(TAG, "欢迎文字==" + text);
                        if (mList != null && mList.size() != 0) {
                                mList.get(0).setTitle(text);
                        }
                        else {
                                shoppingGuideWelcome = new MerchandiseShoppingGuideWelcome();
                                //uuid
//                                String uuid = UUID.randomUUID().toString().replace("-", "");
//                                shoppingGuideWelcome.setId(uuid);
//                                shoppingGuideWelcome.setId("create");
                                shoppingGuideWelcome.setTitle(text);
                        }
                }
        }

        public void delete(View view) {
                //返回 导购子功能页面  不是 应用设置界面
                finish();
                overridePendingTransition(R.anim.face_zoom_enter,
                        R.anim.face_zoom_exit);
        }

        private static final int TIMEOUT = 100;//超时时间100s

        /**
         * 设置超时
         * @return
         */
        private OkHttpClient setTimeOut(){
                OkHttpClient client = new OkHttpClient.Builder()
                        .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                        .build();
                return client;

        }
}
